var fs=require('fs');

exports.add=[
{
	_type:"controller",
	_config_details: {	
	},
	_config: {
		path:"adminpanel/",
		name:"AdminSitemap",
		admin:1,
		role_code:"settings"
	},
	
	_admin_nav: [
		{section:"settings",name:"Sitemap.xml",mode:"index"}
	],

	/**
	 * Controller: AdminSitemap.index 
	 *
	 * Returns:
	 *		typeof HTML
	 */
	index: function(config) {
		
		return this.Cview();
	},

	/**
	 * Controller: AdminSitemap.edit
	 *
	 * Returns:
	 *		typeof HTML
	 */
	edit: function(config) {
		
		return this.Cview();
	},

	/**
	 * Controller: AdminSitemap.save
	 */
	save: function(config) {
		
		return JSON.stringify({redirect:{url:this.action}});
	},

	/**
	 * Controller: AdminSitemap.generate
	 */
	generate: function(config) {
		var t=this;
		var pages=[];
		this.site.models.Page.ListHier("list_nav_level").forEach(function(page) {
			pages.push({loc:page.alias});
		});

		var domain="http://"+this.site.names[0];
		var r="";
		r+='<?xml version="1.0" encoding="UTF-8"?>\n';
		r+='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
		pages.forEach(function(p) {
			r+='\t<url>\n';
			r+='\t\t<loc>'+domain+p.loc+'</loc>\n';
			r+='\t</url>\n';
		});
		r+='</urlset>\n';
		return r;
	},

	/**
	 * Controller: AdminSitemap.write
	 */
	write: function(config) {
		var file=new fs.File(this.site.paths.html+"sitemap.xml");
		file.open("w");
		file.write(this.C("AdminSitemap","generate"));
		file.close();
		return this.Cview();
	},

}];

